package com.fr.chicken.service.Impl;


import com.fr.chicken.dao.*;
import com.fr.chicken.facade.base.BaseResponseDTO;
import com.fr.chicken.facade.request.GetUserExchangeListRequsetDTO;
import com.fr.chicken.facade.request.UserExchangeRequsetDTO;
import com.fr.chicken.facade.response.GetExchangeConfigureResponseDTO;
import com.fr.chicken.facade.response.GetUserExchangeListResponseDTO;
import com.fr.chicken.manager.configure.ConfigManager;
import com.fr.chicken.manager.house.HouseManager;
import com.fr.chicken.model.*;
import com.fr.chicken.model.vo.ChickenInfoVo;
import com.fr.chicken.service.ExchangeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("exchangeService")
public class ExchangeServiceImpl implements ExchangeService {

    @Autowired
    private ExchangeConfigureDao exchangeConfigureDao;
    @Autowired
    private PlayerInfoDao playerInfoDao;
    @Autowired
    private HouseInfoDao houseInfoDao;
    @Autowired
    private ExchangeLogDao exchangeLogDao;
    @Autowired
    private HouseManager houseManager;
    @Autowired
    private MessageLogDao messageLogDao;
    @Autowired
    private ConfigManager configManager;

    @Override
    public GetExchangeConfigureResponseDTO getExchangeConfigure() {
        GetExchangeConfigureResponseDTO responseDTO = new GetExchangeConfigureResponseDTO();
        List<ExchangeConfigure> exchangeConfigures = exchangeConfigureDao.selectAll();
        responseDTO.success();
        responseDTO.setMsg("查询成功");
        responseDTO.setExchangeConfigures(exchangeConfigures);
        return responseDTO;
    }

    @Override
    @Transactional
    public BaseResponseDTO userExchange(UserExchangeRequsetDTO requsetDTO) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        ExchangeLog exchangeLog = new ExchangeLog();
        ExchangeConfigure exchangeConfigure = exchangeConfigureDao.selectByPrimaryKey(requsetDTO.getType());
        Integer num = exchangeConfigure.getProportion()*requsetDTO.getNum();
        PlayerInfo playerInfo = playerInfoDao.selectByPrimaryKey(requsetDTO.getPlayerId());
        PlayerInfo playerInfo1 = new PlayerInfo();
        playerInfo1.setId(requsetDTO.getPlayerId());
        exchangeLog.setStatus((byte)2);
        if (exchangeConfigure.getExchangeType()==1){
            if (exchangeConfigure.getProportionType()==2){
                Integer number = playerInfo.getFeather()-num;
                if (number<0){
                    responseDTO.setMsg("数量不足");
                    responseDTO.faild();
                    return responseDTO;
                }
                playerInfo1.setFeather(number);
            }else if(exchangeConfigure.getProportionType()==3){
                Integer number = playerInfo.getEgg()-num;
                if (number<0){
                    responseDTO.setMsg("数量不足");
                    responseDTO.faild();
                    return responseDTO;
                }
                playerInfo1.setEgg(number);
            }
            playerInfo1.setFeaces(playerInfo.getFeaces()+requsetDTO.getNum());
        }

        else if (exchangeConfigure.getExchangeType()==2){
            if (exchangeConfigure.getProportionType()==1){
                Integer number = playerInfo.getFeaces()-num;
                if (number<0){
                    responseDTO.setMsg("数量不足");
                    responseDTO.faild();
                    return responseDTO;
                }
                playerInfo1.setFeaces(number);
            }else if(exchangeConfigure.getProportionType()==3){
                Integer number = playerInfo.getEgg()-num;
                if (number<0){
                    responseDTO.setMsg("数量不足");
                    responseDTO.faild();
                    return responseDTO;
                }
                playerInfo1.setEgg(number);
            }
            playerInfo1.setFeather(playerInfo.getFeather()+requsetDTO.getNum());
        }

        else if (exchangeConfigure.getExchangeType()==3){
            if (exchangeConfigure.getProportionType()==1){
                Integer number = playerInfo.getFeaces()-num;
                if (number<0){
                    responseDTO.setMsg("数量不足");
                    responseDTO.faild();
                    return responseDTO;
                }
                playerInfo1.setFeaces(number);
            }else if (exchangeConfigure.getProportionType()==2){
                Integer number = playerInfo.getFeather()-num;
                if (number<0){
                    responseDTO.setMsg("数量不足");
                    responseDTO.faild();
                    return responseDTO;
                }
                playerInfo1.setFeather(number);
            }
            playerInfo1.setEgg(playerInfo.getEgg()+requsetDTO.getNum());
        }

        else if (exchangeConfigure.getExchangeType()==4){
            if (exchangeConfigure.getProportionType()==1){
                Integer number = playerInfo.getFeaces()-num;
                if (number<0){
                    responseDTO.setMsg("数量不足");
                    responseDTO.faild();
                    return responseDTO;
                }
                playerInfo1.setFeaces(number);
            }else if (exchangeConfigure.getProportionType()==2){
                Integer number = playerInfo.getFeather()-num;
                if (number<0){
                    responseDTO.setMsg("数量不足");
                    responseDTO.faild();
                    return responseDTO;
                }
                playerInfo1.setFeather(number);
            }
            else if(exchangeConfigure.getProportionType()==3){
                Integer number = playerInfo.getEgg()-num;
                if (number<0){
                    responseDTO.setMsg("数量不足");
                    responseDTO.faild();
                    return responseDTO;
                }
                playerInfo1.setEgg(number);
            }
            HouseInfo houseInfo = houseInfoDao.selectByPlayerId(requsetDTO.getPlayerId());
            if (houseManager.isAddChicken(houseInfo.getId(),requsetDTO.getNum())==false){
                responseDTO.setMsg("生态鸡达到上限");
                responseDTO.faild();
                return responseDTO;
            }
            houseManager.addChickens(requsetDTO.getPlayerId(),(byte)1,requsetDTO.getNum());
        }

        else if (exchangeConfigure.getExchangeType()==5){
             if (requsetDTO.getNum()<configManager.getIntConfig(15)){
                 responseDTO.setMsg("小于最低兑换个数");
                 responseDTO.faild();
                 return responseDTO;
             }
            if (exchangeConfigure.getProportionType()==1){
                Integer number = playerInfo.getFeaces()-num;
                if (number<0){
                    responseDTO.setMsg("数量不足");
                    responseDTO.faild();
                    return responseDTO;
                }
                playerInfo1.setFeaces(number);
            }else if (exchangeConfigure.getProportionType()==2){
                Integer number = playerInfo.getFeather()-num;
                if (number<0){
                    responseDTO.setMsg("数量不足");
                    responseDTO.faild();
                    return responseDTO;
                }
                playerInfo1.setFeather(number);
            }
            else if(exchangeConfigure.getProportionType()==3){
                Integer number = playerInfo.getEgg()-num;
                if (number<0){
                    responseDTO.setMsg("数量不足");
                    responseDTO.faild();
                    return responseDTO;
                }
                playerInfo1.setEgg(number);
            }
            exchangeLog.setStatus((byte)1);
            exchangeLog.setPhone(requsetDTO.getPhone());
            exchangeLog.setName(requsetDTO.getName());
            exchangeLog.setAddress(requsetDTO.getAddress());

            MessageLog messageLog = new MessageLog();
            messageLog.setContent("你兑换"+requsetDTO.getNum()+"个实物鸡蛋成功，请耐心等待客服发货");
            messageLog.setSendTime(System.currentTimeMillis() / 1000);
            messageLog.setSenderId((long)0);
            messageLog.setRecipientId(requsetDTO.getPlayerId());
            messageLog.setType((byte)1);
            int num5 = messageLogDao.insertSelective(messageLog);
            if (num5<1) {
                throw new RuntimeException("兑换失败");
            }
        }
        else {
            responseDTO.setMsg("兑换类型不存在");
            responseDTO.faild();
            return responseDTO;
        }
        if (requsetDTO.getType()!=4||requsetDTO.getType()!=5){
            int num4 = playerInfoDao.updateByPrimaryKeySelective(playerInfo1);
            if (num4<1) {
                throw new RuntimeException("兑换失败");
            }
        }
        String info = exchangeConfigure.getName();
        Long time = System.currentTimeMillis() / 1000;
        exchangeLog.setInfo(info);
        exchangeLog.setTime(time);
        exchangeLog.setPlayerId(requsetDTO.getPlayerId());
        exchangeLog.setType(requsetDTO.getType());
        exchangeLog.setNums(requsetDTO.getNum());
        int num3 = exchangeLogDao.insertSelective(exchangeLog);
        if (num3<1){
            throw new RuntimeException("兑换失败");
        }
        responseDTO.setMsg("兑换成功");
        responseDTO.success();
        return responseDTO;
    }

    @Override
    public GetUserExchangeListResponseDTO getUserExchangeList(GetUserExchangeListRequsetDTO requsetDTO) {
        GetUserExchangeListResponseDTO responseDTO = new GetUserExchangeListResponseDTO();
        if (null != requsetDTO.getPageNum() && null != requsetDTO.getPageSize()) {
            PageHelper.startPage(requsetDTO.getPageNum(), requsetDTO.getPageSize());
        }
        List<ExchangeLog> exchangeLogs =new ArrayList<>();
        if (requsetDTO.getType()>3){
           exchangeLogs = exchangeLogDao.selectByPlayerIdAndMaxType(requsetDTO.getPlayerId());
        }else {
           exchangeLogs = exchangeLogDao.selectByPlayerIdAndMinType(requsetDTO.getPlayerId());
        }
        PageInfo pageInfo = new PageInfo(exchangeLogs);
        pageInfo.setList(exchangeLogs);
        responseDTO.setPageInfo(pageInfo);
        responseDTO.setMsg("查询成功");
        responseDTO.success();
        return responseDTO;
    }

    @Override
    @Transactional
    public BaseResponseDTO setStatus(Integer id) {
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        ExchangeLog exchangeLog = exchangeLogDao.selectByPrimaryKey(id);
        if (exchangeLog.getStatus()==2){
            responseDTO.setMsg("已经设置已收货");
            responseDTO.faild();
            return responseDTO;
        }
        exchangeLog.setStatus((byte)2);
        int num = exchangeLogDao.updateByPrimaryKeySelective(exchangeLog);
        if (num<1){
            throw new RuntimeException("设置失败");
        }
        MessageLog messageLog = new MessageLog();
        messageLog.setContent("你的实物土鸡蛋已发货，请注意查收");
        messageLog.setSendTime(System.currentTimeMillis() / 1000);
        messageLog.setSenderId((long)0);
        messageLog.setRecipientId(exchangeLog.getPlayerId());
        messageLog.setType((byte)1);
        int num5 = messageLogDao.insertSelective(messageLog);
        if (num5<1) {
            throw new RuntimeException("设置失败");
        }
        responseDTO.setMsg("设置成功");
        responseDTO.success();
        return responseDTO;
    }
}
